package;


import com.eclecticdesignstudio.motion.Actuate;
import nme.display.Sprite;
import nme.display.StageAlign;
import nme.display.StageScaleMode;
import nme.events.Event;
import nme.events.MouseEvent;
import nme.geom.Point;
import nme.text.TextField;
import nme.text.TextFormat;
import nme.text.TextFormatAlign;
import nme.Lib;


class WhackAMole extends Sprite {
	
	
	private var Background:Sprite;
	
	private var moles:Array <Mole>;
	private var positions:Array <Point>;
	private var score:Int;
	private var startTime:Int;
	
	
	public function new () {
		
		super ();
		
		initializeGame ();
		startGame ();
		
	}
	
	
	private function createMole ():Void {
		
		var numberOfMoles:Int = 0;
		
		for (mole in moles) {
			
			if (mole != null && mole.active) {
				
				numberOfMoles ++;
				
			}	
				
		}
		
		if (numberOfMoles < 5) {
			
			var index:Int = -1;
			var position:Point = null;
			
			while (position == null) {
					
				index = Math.round (Math.random () * (moles.length - 1));
				
				if (moles[index] == null || !moles[index].active) {
					
					position = positions[index];
						
				}
				
			}
			
			var mole:Mole = new Mole ();
			mole.x = position.x;
			mole.y = position.y;
			mole.addEventListener (MouseEvent.MOUSE_DOWN, Mole_onClick);
			addChild (mole);
				
			moles[index] = mole;
				
		}
		
	}
	
	
	private function endGame ():Void {
		
		removeEventListener (Event.ENTER_FRAME, this_onEnterFrame);
		
		for (mole in moles) {
			
			if (mole != null && mole.active) {
					
				mole.hide ();	
					
			}
			
		}
		
		var endScreen:Sprite = Utils.loadGraphic ("images/TimeUp.png", true);
		endScreen.alpha = 0;
		addChild (endScreen);
		Actuate.tween (endScreen, 2, { alpha: 1 } );
		
		var scoreText:TextField = new TextField ();
		scoreText.y = 205;
		scoreText.width = 320;
		scoreText.text = Std.string (score);
		scoreText.selectable = false;
		
		var scoreFormat:TextFormat = new TextFormat ("_sans", 30, 0xDDDDDD);
		scoreFormat.align = TextFormatAlign.CENTER;
		
		scoreText.setTextFormat (scoreFormat);
		scoreText.alpha = 0;
		Actuate.tween (scoreText, 1, { alpha: 1 } ).delay (0.4);
		addChild (scoreText);
		
	}
	
	
	private function initializeGame ():Void {
		
		Lib.current.stage.align = StageAlign.TOP_LEFT;
		Lib.current.stage.scaleMode = StageScaleMode.NO_SCALE;
		
		positions = new Array <Point> ();
		positions.push (new Point (34, 58));
		positions.push (new Point (99, 68));
		positions.push (new Point (178, 56));
		positions.push (new Point (256, 76));
		positions.push (new Point (52, 118));
		positions.push (new Point (123, 135));
		positions.push (new Point (188, 116));
		positions.push (new Point (242, 137));
		positions.push (new Point (18, 174));
		positions.push (new Point (81, 174));
		positions.push (new Point (164, 193));
		positions.push (new Point (242, 193));
		positions.push (new Point (41, 230));
		positions.push (new Point (118, 246));
		positions.push (new Point (195, 229));
		positions.push (new Point (53, 284));
		positions.push (new Point (122, 303));
		positions.push (new Point (194, 303));
		positions.push (new Point (18, 340));
		positions.push (new Point (88, 355));
		positions.push (new Point (163, 356));
		positions.push (new Point (265, 341));
		positions.push (new Point (255, 267));
		
		moles = new Array <Mole> ();
		
		for (i in 0...positions.length) {
			
			moles.push (null);
			
		}
		
		Background = Utils.loadGraphic ("images/Background.jpg", true);
		addChild (Background);
		
	}
	
	
	private function removeMole (mole:Mole):Void {
		
		mole.removeEventListener (MouseEvent.CLICK, Mole_onClick);
		
		for (i in 0...moles.length) {
			
			if (moles[i] == mole) {
				
				moles[i] == null;
				
			}
			
		}
		
		removeChild (mole);
			
	}
	
	
	private function startGame ():Void {
		
		score = 0;
		startTime = Lib.getTimer ();
		addEventListener (Event.ENTER_FRAME, this_onEnterFrame);
		
	}
	
	
	
	
	// Event Handlers
	
	
	
	
	private function Mole_onClick (event:MouseEvent):Void {
		
		var mole:Mole = cast (event.currentTarget, Mole);
		
		if (mole.active) {
			
			score += mole.points;
			mole.hide (true);
			
		}
		
	}
	
	
	private function this_onEnterFrame (event:Event):Void {
		
		var elapsedTime:Float = (Lib.getTimer () - startTime) / 1000;
		
		if (elapsedTime < 60) {
			
			if (Math.random () < 0.01 * (1 + (elapsedTime * 0.25))) {
				
				createMole ();
				
			}
			
			for (mole in moles) {
				
				if (mole != null) {
					
					mole.step ();
				
					if (mole.dispose && mole.parent != null) {
						
						removeMole (mole);
						
					}
				
				}
				
			}
				
		} else {
			
			endGame ();
			
		}
		
	}
	
	
	
	
	public static function main () {
		
		Lib.current.addChild (new WhackAMole ());
		
	}
	
	
}
